﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class systemconfig_editUpload : System.Web.UI.Page
{
    public string UpPath = "/picture/file/" + String.Format("{0}/{1}/{2}/", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBrid();
        }
    }

    private void DataBrid()
    {
        if (Request["Id"] != null)
        {
            int id = int.Parse(Request.QueryString["Id"].ToString());
            string sqlText = "select * from TuPIanKu where Id = @Id";
            SqlParameter[] param = { new SqlParameter("@Id", SqlDbType.Int) };
            param[0].Value = id;
            SqlDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sqlText, param);
            if (dr.Read())
            {
                this.img_title.Text = dr["font_img"] != null ? dr["font_img"].ToString() : string.Empty;
                this.order_Index.Text = dr["img_order"] != null ? dr["img_order"].ToString() : "0";
                this.img.Src = dr["LuJing"] != null ? dr["LuJing"].ToString() : string.Empty;
                this.show.SelectedValue = dr["visible"] != null ? dr["visible"].ToString() : string.Empty;
            }
        }
        else
        {
            Response.Write("<script>alert('文件不存在');location.href = 'Upload_img.aspx';</script>");
        }
    }


    protected void btn_add_Click(object sender, EventArgs e)
    {
        string uploadname = string.Empty;
        if (filename.FileName != null)
        {
            uploadname = filename.FileName;
            filename.MoveTo(GetPath() + uploadname, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
            uploadname = UpPath + uploadname;
        }
        else
        {
            uploadname = this.img.Src;
        }
          
            string sqlText = "update TuPIanKu set img_order = @img_order,font_img = @font_img,visible = @visible,LuJing = @LuJing where Id= @Id";
            SqlParameter[] param = { new SqlParameter("@Id",SqlDbType.Int,4),  
                                     new SqlParameter("@img_order",SqlDbType.Int,4),                              
                                     new SqlParameter("@font_img",SqlDbType.NVarChar,50),
                                     new SqlParameter("@visible",SqlDbType.Int,4),
                                     new SqlParameter("@LuJing",SqlDbType.VarChar,100)
                                };
            param[0].Value = int.Parse(Request.QueryString["Id"].ToString());
            param[1].Value = Convert.ToInt32(order_Index.Text.Trim());
            param[2].Value = img_title.Text.Trim();           
            param[3].Value = Convert.ToInt32(this.show.SelectedValue);
            param[4].Value = uploadname;

            int id = DbHelper.ExecuteNonQuery(CommandType.Text, sqlText, param);
            if (id > 0)
            {
                Response.Write("<script>alert('修改成功');location.href='Upload_img.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('添加失败');location.href='Upload_img.aspx'</script></script>");
            }
       
    }




    public string GetPath()
    {

        string path = Server.MapPath("~") + UpPath;
        if (!Directory.Exists(path))
        {
            try
            {
                Directory.CreateDirectory(path);
            }

            catch
            {
            }
        }
        return path;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Upload_img.aspx");
    }
}
